package ext.vpde.cadvaulting.migration;

import java.io.*;
import java.util.Calendar;
import java.util.StringTokenizer;

import ext.vpde.cadvaulting.util.WINDSQL;

public class CreateUsageLink {
	
	public static void main ( String [] args ) {
		if (args.length != 1) {
			System.out.println("\nUsage: windchill ext.productcenter.CreateUsageLink loadfile");
		} else {	
			readEntries(args[0]);
		}
    }
	
	private static void readEntries(String inputFile) {
		System.out.println("BEGIN CREATE LINKS: " + Calendar.getInstance().getTime());
		try {
			WINDSQL.openConnection();
			BufferedReader file = LoadUtil.openInputFile(inputFile);
			if (file != null) {
				String s = file.readLine();
				int lineNumber = -1;
				while (s!= null && s.length() > 0) {	
					StringTokenizer strTok = new StringTokenizer(s,"|");
					String pName    = strTok.nextToken();
					String pVer     = strTok.nextToken();
					String pIter    = strTok.nextToken();
									  strTok.nextToken();
					String cName    = strTok.nextToken();
					String cVer  = strTok.nextToken();
					String cIter = strTok.nextToken();
			
					String partStr = pName + ", " + pVer + "." + pIter;
					String refType = "EXTERNAL_REFERENCE";
					if (pName.endsWith(".prt") && cName.endsWith(".asm")) refType = "USAGE";
					if (pName.endsWith(".asm") && cName.endsWith(".prt")) refType = "USAGE";
					if (pName.endsWith(".asm") && cName.endsWith(".asm")) refType = "USAGE";
					if (pName.endsWith(".drw") && cName.endsWith(".asm")) refType = "DRAWING";
					if (pName.endsWith(".drw") && cName.endsWith(".prt")) refType = "DRAWING";
					if (pName.endsWith(".mfg") && cName.endsWith(".asm")) refType = "MANUFACTURING";
					if (pName.endsWith(".drw") && cName.endsWith(".frm")) refType = "DRAWING_FORMAT";
					if (pName.endsWith(".asm") && cName.endsWith(".frm")) refType = "DRAWING_FORMAT";
					if (pName.endsWith(".prt") && cName.endsWith(".lay")) refType = "DRAWING_OVERLAY";
					if (pName.endsWith(".prt") && cName.endsWith(".prt")) refType = "EXTERNAL_REFERENCE";

					String parentIda2a2 = WINDSQL.getIdFromDocumentName(pName,pVer,pIter);
					String childIda2a2  = WINDSQL.getIdOfDocumentMaster(cName);
					
					if (parentIda2a2 == null) {
						System.out.println("PARENT: " + pName + " " + pVer + "." + pIter + " NOT FOUND");
					} else {
						if (childIda2a2 == null) {
							System.out.println("CHILD: " + cName + " NOT FOUND");
						} else {
							if (refType.equals("USAGE")) WINDSQL.createMemberLink(parentIda2a2, partStr, childIda2a2, cName);
							else   {
								childIda2a2  = WINDSQL.getIdFromDocumentName(cName,cVer,cIter);
								WINDSQL.createReferenceLink(parentIda2a2, partStr, childIda2a2, cName, refType);
							}
						}
					}
					s = file.readLine();
					LoadUtil.printProgress(++lineNumber);
				}
			}
			WINDSQL.closeConnection();
		} catch (Exception e) { LoadUtil.printException("CreateLink.readEntries", e); }
		System.out.println("-END- CREATE LINKS: " + Calendar.getInstance().getTime());
	}
}